﻿using System.Collections.Generic;

public class ActionEraser {
	// === Public =====================================================================================================
	public static void DeleteObjectUsingEraser() {
		var transparentObjectPos = MapEditorBaseData.GetInstance.TransparentObject.ElementModel.Position;
		var foundObjects = new List<ElementCtrl>();
		foreach (var editorElement in Game.GetGameModel().Elements) {
			if (transparentObjectPos.x.Equals(editorElement.ElementModel.Position.x) && 
				transparentObjectPos.z.Equals(editorElement.ElementModel.Position.z)) {
				foundObjects.Add(editorElement);
			}
		}
		if (foundObjects.Count == 0) {
			return;
		}
		foreach (var editorElement in foundObjects) {
			if (_fixedHeight.Equals(editorElement.ElementModel.Position.y) || 
				_fixedHeight < editorElement.ElementModel.Position.y) {
				DestroyObject(editorElement);
				return;
			}
		}
	}

	public static void FixHeight() {
		var transparentObjectPos = MapEditorBaseData.GetInstance.TransparentObject.ElementModel.Position;
		_fixedHeight = -0.5f;
		foreach (var element in Game.GetGameModel().Elements) {
			if (transparentObjectPos.x.Equals(element.ElementModel.Position.x) && 
				transparentObjectPos.z.Equals(element.ElementModel.Position.z)) {
				if (_fixedHeight < element.ElementModel.Position.y) {
					_fixedHeight = element.ElementModel.Position.y;
				}
			}
		}
	}

	// === Private ====================================================================================================
	private static float _fixedHeight;

	private static void DestroyObject(ElementCtrl element) {
		Tools.GameObject.Destroy(element.ElementView);
		Game.GetGameModel().RemoveElement(element);
	}
}
